package UILag;
import java.util.Scanner;
import ctrLag.*;
import modelLag.*;

/**
 * Write a description of class FriendsUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI {
    // instance variables - replace the example below with your own
    private LoanCtr loanCtr;

    /**
     * Constructor for objects of class FriendsUI
     */
    public LoanUI() {
        loanCtr = new LoanCtr();
    }

    /**
     * 
     */
    public void start() {
        loanMenu();
    }

    public void loanMenu() {
        boolean exit = false;
        while(!exit) { //that is: while exit is false
            int choise = writeLoanMenu(); 
            if(choise == 1) {
                createLoan();
            }
            else if(choise == 2) {
                findLoan();
            }
            else if(choise == 3) {
                updateLoan();
            } 
            else if(choise == 4) {
                deleteLoan();
            }
            else
            {exit = true;}
        }//end else    
    }//end while

    public int writeLoanMenu() {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Loan Menu ***");
        System.out.println(" (1) Create Loan");
        System.out.println(" (2) Find Loan");
        System.out.println(" (3) Update Loan");
        System.out.println(" (4) Delete Loan");
        System.out.println(" (5) Close");
        System.out.print("\n Make your choice: ");

        int choise = keyboard.nextInt();
        if(choise > 5 || choise == 0) {
            System.out.println("WTF.. You choose " + choise + " this is not a valid number.......");
            return writeLoanMenu();
        }
        else {
            return choise;
        }
    }

    public String inputLoanName() {
        // makes an object keyboard to read input from console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the loan name: ");
        String loanNameInput = keyboard.nextLine();
        return loanNameInput;
    }

    public int inputSerialNumber() {
        // makes an object keyboard to read input from the console
        boolean ok = false;
        int serialNumberInput = 0;
        while(!ok) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Enter the serial number: ");
            try {
                serialNumberInput = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e) {
                System.out.println("Input must be a number, please try again");
                String input = keyboard.nextLine();
            }
        }
        return serialNumberInput;
    }

    public String inputLoanDate() {
        // makes an object keyboard to read input from the console  
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the loan date: ");
        String loanDateInput = keyboard.nextLine();
        return loanDateInput;
    }

    public String inputReturnDate() {
        // makes an object keyboard to read input from the console    
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Enter the return date: ");
        String returnDateInput = keyboard.nextLine();
        return returnDateInput;
    }

    private void createLoan() {
        String loanName = inputLoanName();
        int serialNumber = inputSerialNumber();
        String loanDate = inputLoanDate();
        String returnDate = inputReturnDate();

        loanCtr.addLoan(serialNumber, loanName, loanDate, returnDate);
    }

    private Loan findLoan() {
        int serialNumber = inputSerialNumber();
        Loan loan = loanCtr.findLoan(serialNumber);

        try {
            loan.printLoanInfo();
            return loan;
        }
        catch (Exception e) {
            System.out.println("Error! Serial is not registered in system." + "\n");
        }
        return loan;
    }

    public void updateLoan() {
        int serialNumber = inputSerialNumber();
        Loan loan = loanCtr.findLoan(serialNumber);
        if (loan != null) {
            loan.printLoanInfo();
            String newLoanName = inputLoanName();
            String newLoanDate = inputLoanDate();
            String newReturnDate = inputReturnDate();
            int newSerialNumber = inputSerialNumber();
            loanCtr.updateLoan(loan, newSerialNumber, newLoanDate, newReturnDate, newLoanName);
        }
        else {
            System.out.println("Error! Person is not registered in system." + "\n");
        }
    }

    public void deleteLoan() {
        int serialNumber = inputSerialNumber();
        loanCtr.deleteLoan(serialNumber);
    }
}